import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import { User } from '@/types/account';
//import axios from 'axios';

// 管理员类型常量
export enum AdminType {
  REGULAR_USER = "Regular User",
  ADMIN = "Admin",
  SUPER_ADMIN = "Super Admin"
}

interface AuthState {
  token: string | null;
  user: User | null;
  isAuthenticated: boolean;
  isAdmin: boolean; // 改为普通属性而不是getter
  setAuth: (token: string, user: User) => void;
  clearAuth: () => void;
}

export const useAuthStore = create<AuthState>()(
  persist(
    (set, get) => ({
      token: null,
      user: null,
      isAuthenticated: false,
      isAdmin: false,
      setAuth: (token: string, user: User) => {
        const isAdmin = user.admin_type === AdminType.ADMIN || user.admin_type === AdminType.SUPER_ADMIN;
        set({ token, user, isAuthenticated: true, isAdmin });
        //axios.defaults.headers.common['Authorization'] = `Token ${token}`;
      },
      clearAuth: () => {
        set({ token: null, user: null, isAuthenticated: false, isAdmin: false });
        //delete axios.defaults.headers.common['Authorization'];
      },
    }),
    {
      name: 'auth-storage',
      storage: createJSONStorage(() => localStorage), // 明确使用localStorage
      partialize: (state) => ({
        token: state.token,
        user: state.user,
        isAuthenticated: state.isAuthenticated,
        isAdmin: state.isAdmin,
      }), // 仅持久化这些字段
    }
  )
); 